package org.jaronsource.framework.plugins.security.web.taglib;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import org.jaronsource.framework.plugins.security.dao.IPermDao;
import org.jaronsource.framework.plugins.security.domain.IPerm;

public class PermTag extends TagSupport {

	private static final long serialVersionUID = 3498319010712403041L;

	private String code;

	private String var;

	public String getCode() {
		return code;
	}

	public void setCode( String code ) {
		this.code = code;
	}

	public String getVar() {
		return var;
	}

	public void setVar( String var ) {
		this.var = var;
	}

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		ServletContext sc = request.getSession().getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext( sc );

		IPermDao PermDao = wac.getBean( IPermDao.class );
		IPerm perm = PermDao.findPermByCode( getCode() );

		pageContext.setAttribute( var, perm );
		return SKIP_BODY;
	}

}
